package com.ruoyi.zyx.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zyx.domain.ZyxAllot;
import com.ruoyi.zyx.domain.ZyxCheck;
import com.ruoyi.zyx.domain.ZyxTakecare;
import com.ruoyi.zyx.domain.ZyxWarn;
import com.ruoyi.zyx.mapper.ZyxAllotMapper;
import com.ruoyi.zyx.mapper.ZyxCheckMapper;
import com.ruoyi.zyx.mapper.ZyxTakecareMapper;
import com.ruoyi.zyx.service.impl.ZyxAllotServiceImpl;
import com.ruoyi.zyx.service.impl.ZyxCheckServiceImpl;
import com.ruoyi.zyx.service.impl.ZyxTakecareServiceImpl;
import com.ruoyi.zyx.service.impl.ZyxWarnServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("zyxTakecareTask")
public class ZyxTakecareTask {
    @Autowired private ZyxTakecareMapper zyxTakecareMapper;
    @Autowired private ZyxWarnServiceImpl zyxWarnService;
    @Autowired private ZyxCheckMapper zyxCheckMapper;
    @Autowired private ZyxAllotMapper zyxAllotMapper;

    /**
     * 保养
     */
    public void takecare(){
//        System.out.println("执行");
        ZyxTakecare zyxTakecare=new ZyxTakecare();
        zyxTakecare.setIswarn("N");//未提醒的
        List<ZyxTakecare> zyxTakecares=zyxTakecareMapper.selectZyxTakecareList(zyxTakecare);
        for (ZyxTakecare takecare : zyxTakecares) {
            //校验是否该到期提醒了
            if(DateUtils.isSameDay(takecare.getNextdate(),new Date())){
                ZyxWarn zyxWarn=new ZyxWarn();
                zyxWarn.setDepartid(takecare.getDepartid());
                zyxWarn.setDeviceid(takecare.getDeviceid());
                zyxWarn.setIsread("N");
                zyxWarn.setServieid(takecare.getId());
                zyxWarn.setWarndate(takecare.getNextdate());
                zyxWarn.setWarntype("1");
                zyxWarnService.insertZyxWarn(zyxWarn);
                takecare.setIswarn("Y");
                zyxTakecareMapper.updateZyxTakecare(takecare);
            }

        }
    }

    /**
     * 检定
     */
    public void check(){
        ZyxCheck zyxCheck=new ZyxCheck();
        zyxCheck.setIswarn("N");//未提醒的
        List<ZyxCheck> zyxChecks=zyxCheckMapper.selectZyxCheckList(zyxCheck);
        for (ZyxCheck check : zyxChecks) {
            if(DateUtils.isSameDay(check.getNextdate(),new Date())){
                ZyxWarn zyxWarn=new ZyxWarn();
                zyxWarn.setDepartid(check.getDepartid());
                zyxWarn.setDeviceid(check.getDeviceid());
                zyxWarn.setIsread("N");
                zyxWarn.setServieid(check.getId());
                zyxWarn.setWarndate(check.getNextdate());
                zyxWarn.setWarntype("2");//检定
                zyxWarnService.insertZyxWarn(zyxWarn);
                check.setIswarn("Y");
                zyxCheckMapper.updateZyxCheck(check);
            }
        }
    }

    /**
     * 借调
     */
    public void allot(){
        ZyxAllot zyxAllot=new ZyxAllot();
        zyxAllot.setIswarn("N");
        List<ZyxAllot> zyxAllots=zyxAllotMapper.selectZyxAllotList(zyxAllot);
        for (ZyxAllot allot : zyxAllots) {
            if(DateUtils.isSameDay(allot.getWarndate(),new Date())){
                ZyxWarn zyxWarn=new ZyxWarn();
                zyxWarn.setDepartid(allot.getDepartid());
                zyxWarn.setDeviceid(allot.getDeviceid());
                zyxWarn.setIsread("N");
                zyxWarn.setServieid(allot.getId());
                zyxWarn.setWarndate(allot.getWarndate());
                zyxWarn.setWarntype("4");//借调
                zyxWarnService.insertZyxWarn(zyxWarn);
                allot.setIswarn("Y");
                zyxAllotMapper.updateZyxAllot(allot);
            }
        }

    }
}
